package com.lanlian.cayman.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.lanlian.cayman.LLApplication;
import com.lanlian.cayman.R;
import com.lanlian.cayman.fragment.UpdateRegisterFourFragment;
import com.lanlian.cayman.fragment.UpdateRegisterOneFragment;
import com.lanlian.cayman.fragment.UpdateRegisterThreeFragment;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ReqGetPreRegister;
import com.lanlian.commonlib.bean.RespPreRegister;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.uikit.ViewPagerSlide;

import java.util.ArrayList;


public class UpdateOpenAccountActivity extends BaseActivity implements OnClickListener {

    private RadioGroup mRgOpenAccount;
    private ViewPagerSlide mViewPager;
    private ImageView mImgBack;

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    public static RespPreRegister respPreRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_account_update);
        initView();
        setListeners();
        initFragments();
        getPreRegisterInfo();
    }

    @Override
    protected void initView() {
        super.initView();
        mRgOpenAccount = findViewById(R.id.rg_open_account);
        mViewPager = findViewById(R.id.view_pager);
        mImgBack = findViewById(R.id.img_back);
        mViewPager.setSlide(false);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mImgBack.setOnClickListener(this);
    }

    private void initFragments() {
        mFragments.add(new UpdateRegisterOneFragment(new UpdateRegisterOneFragment.OnNextClickListener() {
            @Override
            public void onNextClick() {
                UpdateRegisterThreeFragment fragment = (UpdateRegisterThreeFragment) mFragments.get(1);
                fragment.setPreRegisterInfo();
                mViewPager.setCurrentItem(1);
                mRgOpenAccount.check(R.id.rb_assets);
            }
        }));
        mFragments.add(new UpdateRegisterThreeFragment(new UpdateRegisterThreeFragment.OnPreviousClickListener() {
            @Override
            public void onNextClick() {
                UpdateRegisterFourFragment fragment = (UpdateRegisterFourFragment) mFragments.get(2);
                fragment.setPreRegisterInfo();
                mViewPager.setCurrentItem(2);
                mRgOpenAccount.check(R.id.rb_profession);
            }

            @Override
            public void onPreviousClick() {
                mViewPager.setCurrentItem(0);
                mRgOpenAccount.check(R.id.rb_id);
            }
        }));
        mFragments.add(new UpdateRegisterFourFragment(new UpdateRegisterFourFragment.OnPreviousClickListener() {
            @Override
            public void onPreviousClick() {
                mViewPager.setCurrentItem(1);
                mRgOpenAccount.check(R.id.rb_assets);
            }
        }));
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return mFragments.get(i);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        });
        mRgOpenAccount.check(R.id.rb_id);
        mViewPager.setOffscreenPageLimit(mFragments.size());
    }

    /**
     * 获取预开户信息
     */
    public void getPreRegisterInfo() {
        ReqGetPreRegister reqGetPreRegister = new ReqGetPreRegister();
        reqGetPreRegister.setMobile(LLApplication.reqUpdateUserRegister.getMobile());
        VolleyRequestUtils.getInstance().getPreRegister(this.toString(), reqGetPreRegister, new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                respPreRegister = (RespPreRegister) response;
                UpdateRegisterOneFragment fragment = (UpdateRegisterOneFragment) mFragments.get(0);
                fragment.setPreRegisterInfo();
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.img_back:
                finish();
                break;
            default:
                break;
        }
    }

}
